package com.ctshk.rpc.hotel.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.thread.ThreadUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SaleOnShelfStatusCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.DateUtil;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.hotel.dto.*;
import com.ctshk.rpc.hotel.entity.*;
import com.ctshk.rpc.hotel.mapper.*;
import com.ctshk.rpc.hotel.req.*;
import com.ctshk.rpc.hotel.service.IHotelH5Service;
import com.ctshk.rpc.hotel.service.IHotelProductStrategyService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 酒店产品加幅策略 服务实现类
 * </p>
 *
 * @author yinhuan
 * @since 2021-03-03
 */
@DubboService
public class HotelProductStrategyServiceImpl extends ServiceImpl<HotelProductStrategyMapper, HotelProductStrategy> implements IHotelProductStrategyService {
    @Autowired
    private HotelProductStrategyMapper hotelProductStrategyMapper;
    @Autowired
    private HotelProductMapper hotelProductMapper;
    @Autowired
    private HotelMapper hotelMapper;
    @Autowired
    private HotelProductStrategyDateMapper hotelProductStrategyDateMapper;
    @Autowired
    private HotelProductChannelOfflineAgentCustomMapper hotelProductChannelOfflineAgentCustomMapper;
    @Autowired
    private HotelProductIncreaseStrategyRuleMapper hotelProductIncreaseStrategyRuleMapper;
    @Autowired
    private HotelProductChannelOfflineSelfCustomMapper hotelProductChannelOfflineSelfCustomMapper;
    @Autowired
    private HotelProductChannelOnlineSelfPlatformMapper hotelProductChannelOnlineSelfPlatformMapper;
    @Autowired
    private HotelProductChannelOnlineThirdPlatformMapper hotelProductChannelOnlineThirdPlatformMapper;
    @Autowired
    private HotelRoomTypeMapper hotelRoomTypeMapper;
    @Autowired
    private IHotelH5Service hotelH5Service;
    /**
     * 1-上架  2-下架
     */
    public final static Integer SHELVES = 1;
    public final static Integer UN_SHELVES = 2;

    /**
     * 1-推荐
     */
    public final static Integer RECOMMENDED = 1;
    public final static Integer UN_RECOMMENDED = 2;

    public final static Integer SALE_CHANNEL_TYPE_DISABLED = 1; //销售渠道类型：禁售
    public final static Integer SALE_CHANNEL_TYPE_ALL = 2; //销售渠道类型：所有
    public final static Integer SALE_CHANNEL_TYPE_SELECTED = 3; //销售渠道类型：选中的
    public final static Integer SALE_TYPE_UNLIMITED = 1; // 销售数量类型：无限制
    public final static Integer SALE_TYPE_LIMIT = 2; // 销售数量类型：有限制

    /**  1-待提交  2-审批中 3-审批通过 4-审批不通过*/
    public final static Integer UN_APPROVE = 1;
    public final static Integer APPROVE_ING  = 2;
    public final static Integer APPROVED  = 3;
    public final static Integer REJECTED  = 4;
    /**  0-未删除  1-已删除*/
    public final static Integer NOT_DELETE = 0;
    public final static Integer IS_DELETED = 1;
    /**
     * 产品中心-分页查询产品列表
     * @param req
     * @return
     */
    @Override
    public PageResponse<HotelProductListDTO> queryList(HotelListProductReq req) {
        Page<HotelProductListDTO> page = new Page<>(req.getPageNo(), req.getPageSize());
        Page<HotelProductListDTO> iPage = hotelProductMapper.queryHotelProductListByStatus(req,page);
        List<HotelProductListDTO> records = page.getRecords();
        if(CollectionUtils.isEmpty(records)){
            return new PageResponse<>(new ArrayList<>(), iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
        }
        return new PageResponse<>(records, iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
    }

    /**
     * 校验产品加幅信息入参
     * @param reqList
     * @param user
     * @return
     */
    private Result checkProductStrategyReq(List<HotelProductStrategyReq> reqList, TokenUser user){
        if(Objects.isNull(user)){
            return Result.failed(SystemError.USER_1000);
        }
        if(CollectionUtils.isEmpty(reqList)){
            return Result.failed(SystemError.HOTEL_10017);
        }
        return null;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result save(Long hotelProductId, List<HotelProductStrategyReq> hotelProductStrategyReqList, Long userId) {
        // 校验
        Result checkResult = checkStrategyDateResult(hotelProductStrategyReqList);
        if (null != checkResult) {
            return checkResult;
        }

        // 插入加幅策略列表
        List<HotelProductStrategy> strategyInsertList = new ArrayList<>();
        List<HotelProductStrategyDate> strategyDateInsertList = new ArrayList<>();
        List<HotelProductIncreaseStrategyRule> strategyRuleInsertList = new ArrayList<>();
        List<HotelProductChannelOfflineSelfCustom> offlineSelfCustomInsertList = new ArrayList<>();
        List<HotelProductChannelOfflineAgentCustom> offlineAgentCustomInsertList = new ArrayList<>();
        List<HotelProductChannelOnlineSelfPlatform> onlineSelfPlatformInsertList = new ArrayList<>();
        List<HotelProductChannelOnlineThirdPlatform> onlineThirdPlatformInsertList = new ArrayList<>();
        // 更新加幅策略列表
        List<HotelProductStrategy> strategyUpdateList = new ArrayList<>();
        List<HotelProductStrategyDate> strategyDateUpdateList = new ArrayList<>();
        List<HotelProductIncreaseStrategyRule> strategyRuleUpdateList = new ArrayList<>();
        List<HotelProductChannelOfflineSelfCustom> offlineSelfCustomUpdateList = new ArrayList<>();
        List<HotelProductChannelOfflineAgentCustom> offlineAgentCustomUpdateList = new ArrayList<>();
        List<HotelProductChannelOnlineSelfPlatform> onlineSelfPlatformUpdateList = new ArrayList<>();
        List<HotelProductChannelOnlineThirdPlatform> onlineThirdPlatformUpdateList = new ArrayList<>();
        // 不需删除加幅策略ID列表
        List<Long> strategyIdList = new ArrayList<>();
        List<Long> strategyDateIdList = new ArrayList<>();
        List<Long> strategyRuleIdList = new ArrayList<>();
        List<Long> offlineSelfCustomIdList = new ArrayList<>();
        List<Long> offlineAgentCustomIdList = new ArrayList<>();
        List<Long> onlineSelfPlatformIdList = new ArrayList<>();
        List<Long> onlineThirdPlatformIdList = new ArrayList<>();
        LocalDateTime nowDateTime = LocalDateTime.now();

        // 查询当前酒店-策略IDList
        List<Long> deleteStrategyIdList = hotelProductStrategyMapper.selectIdListByHotelProductIds(Arrays.asList(hotelProductId));
        for (HotelProductStrategyReq strategyReq : hotelProductStrategyReqList) {
            // 构建加幅策略
            HotelProductStrategy hotelProductStrategy = EntityUtil.copy(strategyReq, HotelProductStrategy.class);
            if (hotelProductStrategy.getId() == null) {
                hotelProductStrategy.setId(SnowflakeIdWorker.nextId());
                strategyInsertList.add(hotelProductStrategy);
            } else {
                // 剔除传过来的策略ID，剩下的是需要删除的
                if (deleteStrategyIdList.contains(hotelProductStrategy.getId())) {
                    deleteStrategyIdList.remove(hotelProductStrategy.getId());
                }
                strategyUpdateList.add(hotelProductStrategy);
                strategyIdList.add(hotelProductStrategy.getId());
            }
            hotelProductStrategy.setHotelProductId(hotelProductId);
            // 构建加幅策略日期段
            List<HotelProductStrategyDate> increaseStrategyDateList = EntityUtil.copyList(strategyReq.getHotelProductStrategyDateReqList(), HotelProductStrategyDate.class);
            for (HotelProductStrategyDate strategyDate : increaseStrategyDateList) {
                if (strategyDate.getId() == null) {
                    strategyDate.setId(SnowflakeIdWorker.nextId());
                    strategyDateInsertList.add(strategyDate);
                } else {
                    strategyDateUpdateList.add(strategyDate);
                    strategyDateIdList.add(strategyDate.getId());
                }
                strategyDate.setIncreaseStrategyId(hotelProductStrategy.getId());
            }
            // 构建加幅策略-线下自营规则-所有
            if (hotelProductStrategy.getSelfSalePermissionType() == SALE_CHANNEL_TYPE_ALL) {
                HotelProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(strategyReq.getHotelProductIncreaseStrategySelfRule(), HotelProductIncreaseStrategyRule.class);
                if (increaseStrategyRule.getId() == null) {
                    increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                    strategyRuleInsertList.add(increaseStrategyRule);
                } else {
                    strategyRuleUpdateList.add(increaseStrategyRule);
                    strategyRuleIdList.add(increaseStrategyRule.getId());
                }
                increaseStrategyRule.setIncreaseStrategyId(hotelProductStrategy.getId());
                increaseStrategyRule.setSoldNumber(null);
                hotelProductStrategy.setSelfSalePermissionId(increaseStrategyRule.getIncreaseStrategyId());
            }
            // 构建加幅策略-线下自营规则-指定
            if (strategyReq.getSelfSalePermissionType() == SALE_CHANNEL_TYPE_SELECTED && CollUtil.isNotEmpty(strategyReq.getHotelProductChannelOfflineSelfCustomReqList())) {
                strategyReq.getHotelProductChannelOfflineSelfCustomReqList().forEach(channelOfflineSelfCustomReq -> {
                    HotelProductChannelOfflineSelfCustom channelOfflineSelfCustom = EntityUtil.copy(channelOfflineSelfCustomReq, HotelProductChannelOfflineSelfCustom.class);
                    if (channelOfflineSelfCustom.getId() == null) {
                        channelOfflineSelfCustom.setId(SnowflakeIdWorker.nextId());
                        offlineSelfCustomInsertList.add(channelOfflineSelfCustom);
                    } else {
                        offlineSelfCustomUpdateList.add(channelOfflineSelfCustom);
                        offlineSelfCustomIdList.add(channelOfflineSelfCustom.getId());
                    }
                    channelOfflineSelfCustom.setIncreaseStrategyId(hotelProductStrategy.getId());
                    channelOfflineSelfCustom.setCreateId(userId);
                    channelOfflineSelfCustom.setGmtCreate(nowDateTime);
                    channelOfflineSelfCustom.setModifiedId(userId);
                    channelOfflineSelfCustom.setGmtModified(nowDateTime);
                    channelOfflineSelfCustom.setIsDeleted(IsDeletedCode.NO.getCode());
                    // 插入加幅规则
                    HotelProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOfflineSelfCustomReq.getHotelProductIncreaseStrategyRuleReq(), HotelProductIncreaseStrategyRule.class);
                    if (increaseStrategyRule.getId() == null) {
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        strategyRuleInsertList.add(increaseStrategyRule);
                    } else {
                        strategyRuleUpdateList.add(increaseStrategyRule);
                        strategyRuleIdList.add(increaseStrategyRule.getId());
                    }
                    increaseStrategyRule.setIncreaseStrategyId(hotelProductStrategy.getId());
                    increaseStrategyRule.setCustomId(channelOfflineSelfCustom.getId());
                    increaseStrategyRule.setSoldNumber(null);
                });
            }
            // 构建加幅策略-线下代理规则-所有
            if (hotelProductStrategy.getAgentSalePermissionType() == SALE_CHANNEL_TYPE_ALL) {
                HotelProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(strategyReq.getHotelProductIncreaseStrategyAgentRule(), HotelProductIncreaseStrategyRule.class);
                if (increaseStrategyRule.getId() == null) {
                    increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                    strategyRuleInsertList.add(increaseStrategyRule);
                } else {
                    strategyRuleUpdateList.add(increaseStrategyRule);
                    strategyRuleIdList.add(increaseStrategyRule.getId());
                }
                increaseStrategyRule.setIncreaseStrategyId(hotelProductStrategy.getId());
                increaseStrategyRule.setSoldNumber(null);
                hotelProductStrategy.setSelfSalePermissionId(increaseStrategyRule.getIncreaseStrategyId());
            }
            // 构建加幅策略-线下代理规则-指定
            if (strategyReq.getAgentSalePermissionType() == SALE_CHANNEL_TYPE_SELECTED && CollUtil.isNotEmpty(strategyReq.getHotelProductChannelOfflineAgentCustomReqList())) {
                strategyReq.getHotelProductChannelOfflineAgentCustomReqList().forEach(channelOfflineAgentCustomReq -> {
                    HotelProductChannelOfflineAgentCustom channelOfflineAgentCustom = EntityUtil.copy(channelOfflineAgentCustomReq, HotelProductChannelOfflineAgentCustom.class);
                    if (channelOfflineAgentCustom.getId() == null) {
                        channelOfflineAgentCustom.setId(SnowflakeIdWorker.nextId());
                        offlineAgentCustomInsertList.add(channelOfflineAgentCustom);
                    } else {
                        offlineAgentCustomUpdateList.add(channelOfflineAgentCustom);
                        offlineAgentCustomIdList.add(channelOfflineAgentCustom.getId());
                    }
                    channelOfflineAgentCustom.setIncreaseStrategyId(hotelProductStrategy.getId());
                    channelOfflineAgentCustom.setCreateId(userId);
                    channelOfflineAgentCustom.setGmtCreate(nowDateTime);
                    channelOfflineAgentCustom.setModifiedId(userId);
                    channelOfflineAgentCustom.setGmtModified(nowDateTime);
                    channelOfflineAgentCustom.setIsDeleted(IsDeletedCode.NO.getCode());
                    // 插入加幅规则
                    HotelProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOfflineAgentCustomReq.getHotelProductIncreaseStrategyRuleReq(), HotelProductIncreaseStrategyRule.class);
                    if (increaseStrategyRule.getId() == null) {
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        strategyRuleInsertList.add(increaseStrategyRule);
                    } else {
                        strategyRuleUpdateList.add(increaseStrategyRule);
                        strategyRuleIdList.add(increaseStrategyRule.getId());
                    }
                    increaseStrategyRule.setIncreaseStrategyId(hotelProductStrategy.getId());
                    increaseStrategyRule.setCustomId(channelOfflineAgentCustom.getId());
                    increaseStrategyRule.setSoldNumber(null);
                });
            }
            // 线上自营
            if (CollUtil.isNotEmpty(strategyReq.getHotelProductChannelOnlineSelfPlatformReqList())) {
                strategyReq.getHotelProductChannelOnlineSelfPlatformReqList().forEach(channelOnlineSelfPlatformReq -> {
                    HotelProductChannelOnlineSelfPlatform channelOnlineSelfPlatform = EntityUtil.copy(channelOnlineSelfPlatformReq, HotelProductChannelOnlineSelfPlatform.class);
                    if (channelOnlineSelfPlatform.getId() == null) {
                        channelOnlineSelfPlatform.setId(SnowflakeIdWorker.nextId());
                        onlineSelfPlatformInsertList.add(channelOnlineSelfPlatform);
                    } else {
                        onlineSelfPlatformUpdateList.add(channelOnlineSelfPlatform);
                        onlineSelfPlatformIdList.add(channelOnlineSelfPlatform.getId());
                    }
                    channelOnlineSelfPlatform.setIncreaseStrategyId(hotelProductStrategy.getId());
                    channelOnlineSelfPlatform.setCreateId(userId);
                    channelOnlineSelfPlatform.setGmtCreate(nowDateTime);
                    channelOnlineSelfPlatform.setModifiedId(userId);
                    channelOnlineSelfPlatform.setGmtModified(nowDateTime);
                    channelOnlineSelfPlatform.setIsDeleted(IsDeletedCode.NO.getCode());
                    // 插入加幅规则
                    HotelProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOnlineSelfPlatformReq.getHotelProductIncreaseStrategyRuleReq(), HotelProductIncreaseStrategyRule.class);
                    if (increaseStrategyRule.getId() == null) {
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        strategyRuleInsertList.add(increaseStrategyRule);
                    } else {
                        strategyRuleUpdateList.add(increaseStrategyRule);
                        strategyRuleIdList.add(increaseStrategyRule.getId());
                    }
                    increaseStrategyRule.setIncreaseStrategyId(hotelProductStrategy.getId());
                    increaseStrategyRule.setCustomId(channelOnlineSelfPlatform.getId());
                    increaseStrategyRule.setSoldNumber(null);
                });
            }
            // 线上第三方
            if (CollUtil.isNotEmpty(strategyReq.getHotelProductChannelOnlineThirdPlatformReqList())) {
                strategyReq.getHotelProductChannelOnlineThirdPlatformReqList().forEach(channelOnlineThirdPlatformReq -> {
                    HotelProductChannelOnlineThirdPlatform channelOnlineThirdPlatform = EntityUtil.copy(channelOnlineThirdPlatformReq, HotelProductChannelOnlineThirdPlatform.class);
                    if (channelOnlineThirdPlatform.getId() == null) {
                        channelOnlineThirdPlatform.setId(SnowflakeIdWorker.nextId());
                        onlineThirdPlatformInsertList.add(channelOnlineThirdPlatform);
                    } else {
                        onlineThirdPlatformUpdateList.add(channelOnlineThirdPlatform);
                        onlineThirdPlatformIdList.add(channelOnlineThirdPlatform.getId());
                    }
                    channelOnlineThirdPlatform.setIncreaseStrategyId(hotelProductStrategy.getId());
                    channelOnlineThirdPlatform.setCreateId(userId);
                    channelOnlineThirdPlatform.setGmtCreate(nowDateTime);
                    channelOnlineThirdPlatform.setModifiedId(userId);
                    channelOnlineThirdPlatform.setGmtModified(nowDateTime);
                    channelOnlineThirdPlatform.setIsDeleted(IsDeletedCode.NO.getCode());
                    // 插入加幅规则
                    HotelProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOnlineThirdPlatformReq.getHotelProductIncreaseStrategyRuleReq(), HotelProductIncreaseStrategyRule.class);
                    if (increaseStrategyRule.getId() == null) {
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        strategyRuleInsertList.add(increaseStrategyRule);
                    } else {
                        strategyRuleUpdateList.add(increaseStrategyRule);
                        strategyRuleIdList.add(increaseStrategyRule.getId());
                    }
                    increaseStrategyRule.setIncreaseStrategyId(hotelProductStrategy.getId());
                    increaseStrategyRule.setCustomId(channelOnlineThirdPlatform.getId());
                    increaseStrategyRule.setSoldNumber(null);
                });
            }
        }

        if (CollUtil.isNotEmpty(deleteStrategyIdList)) {// 删除入参中不存在的ID列表
            hotelProductStrategyMapper.delete(new QueryWrapper<HotelProductStrategy>().in("id", deleteStrategyIdList));
            hotelProductStrategyDateMapper.delete(new QueryWrapper<HotelProductStrategyDate>().in("increase_strategy_id", deleteStrategyIdList));
            hotelProductIncreaseStrategyRuleMapper.delete(new QueryWrapper<HotelProductIncreaseStrategyRule>().in("increase_strategy_id", deleteStrategyIdList));
            hotelProductChannelOfflineSelfCustomMapper.delete(new QueryWrapper<HotelProductChannelOfflineSelfCustom>().in("increase_strategy_id", deleteStrategyIdList));
            hotelProductChannelOfflineAgentCustomMapper.delete(new QueryWrapper<HotelProductChannelOfflineAgentCustom>().in("increase_strategy_id", deleteStrategyIdList));
            hotelProductChannelOnlineSelfPlatformMapper.delete(new QueryWrapper<HotelProductChannelOnlineSelfPlatform>().in("increase_strategy_id", deleteStrategyIdList));
            hotelProductChannelOnlineThirdPlatformMapper.delete(new QueryWrapper<HotelProductChannelOnlineThirdPlatform>().in("increase_strategy_id", deleteStrategyIdList));
        }
        if (CollUtil.isNotEmpty(strategyDateIdList)) {// 删除不存在的ID列表
            hotelProductStrategyDateMapper.delete(new QueryWrapper<HotelProductStrategyDate>().notIn("id", strategyDateIdList));
        } else if (CollUtil.isNotEmpty(strategyIdList)) {// 删除不存在的ID列表
            hotelProductStrategyDateMapper.delete(new QueryWrapper<HotelProductStrategyDate>().in("increase_strategy_id", strategyIdList));
        }
        if (CollUtil.isNotEmpty(strategyRuleIdList)) {
            hotelProductIncreaseStrategyRuleMapper.delete(new QueryWrapper<HotelProductIncreaseStrategyRule>().notIn("id", strategyRuleIdList));
        } else if (CollUtil.isNotEmpty(strategyIdList)) {
            hotelProductIncreaseStrategyRuleMapper.delete(new QueryWrapper<HotelProductIncreaseStrategyRule>().in("increase_strategy_id", strategyIdList));
        }
        if (CollUtil.isNotEmpty(offlineSelfCustomIdList)) {
            hotelProductChannelOfflineSelfCustomMapper.delete(new QueryWrapper<HotelProductChannelOfflineSelfCustom>().notIn("id", offlineSelfCustomIdList));
        } else if (CollUtil.isNotEmpty(strategyIdList)) {
            hotelProductChannelOfflineSelfCustomMapper.delete(new QueryWrapper<HotelProductChannelOfflineSelfCustom>().in("increase_strategy_id", strategyIdList));
        }
        if (CollUtil.isNotEmpty(offlineAgentCustomIdList)) {
            hotelProductChannelOfflineAgentCustomMapper.delete(new QueryWrapper<HotelProductChannelOfflineAgentCustom>().notIn("id", offlineAgentCustomIdList));
        } else if (CollUtil.isNotEmpty(strategyIdList)) {
            hotelProductChannelOfflineAgentCustomMapper.delete(new QueryWrapper<HotelProductChannelOfflineAgentCustom>().in("increase_strategy_id", strategyIdList));
        }
        if (CollUtil.isNotEmpty(onlineSelfPlatformIdList)) {
            hotelProductChannelOnlineSelfPlatformMapper.delete(new QueryWrapper<HotelProductChannelOnlineSelfPlatform>().notIn("id", onlineSelfPlatformIdList));
        } else if (CollUtil.isNotEmpty(strategyIdList)) {
            hotelProductChannelOnlineSelfPlatformMapper.delete(new QueryWrapper<HotelProductChannelOnlineSelfPlatform>().in("increase_strategy_id", strategyIdList));
        }
        if (CollUtil.isNotEmpty(onlineThirdPlatformIdList)) {
            hotelProductChannelOnlineThirdPlatformMapper.delete(new QueryWrapper<HotelProductChannelOnlineThirdPlatform>().notIn("id", onlineThirdPlatformIdList));
        } else if (CollUtil.isNotEmpty(strategyIdList)) {
            hotelProductChannelOnlineThirdPlatformMapper.delete(new QueryWrapper<HotelProductChannelOnlineThirdPlatform>().in("increase_strategy_id", strategyIdList));
        }
        // 更新
        updateBatchSomeColumn(strategyUpdateList, hotelProductStrategyMapper);
        updateBatchSomeColumn(strategyDateUpdateList, hotelProductStrategyDateMapper);
        updateBatchSomeColumn(strategyRuleUpdateList, hotelProductIncreaseStrategyRuleMapper);
        updateBatchSomeColumn(offlineSelfCustomUpdateList, hotelProductChannelOfflineSelfCustomMapper);
        updateBatchSomeColumn(offlineAgentCustomUpdateList, hotelProductChannelOfflineAgentCustomMapper);
        updateBatchSomeColumn(onlineSelfPlatformUpdateList, hotelProductChannelOnlineSelfPlatformMapper);
        updateBatchSomeColumn(onlineThirdPlatformUpdateList, hotelProductChannelOnlineThirdPlatformMapper);
        // 插入
        insertBatchSomeColumn(strategyInsertList, hotelProductStrategyMapper);
        insertBatchSomeColumn(strategyDateInsertList, hotelProductStrategyDateMapper);
        insertBatchSomeColumn(strategyRuleInsertList, hotelProductIncreaseStrategyRuleMapper);
        insertBatchSomeColumn(offlineSelfCustomInsertList, hotelProductChannelOfflineSelfCustomMapper);
        insertBatchSomeColumn(offlineAgentCustomInsertList, hotelProductChannelOfflineAgentCustomMapper);
        insertBatchSomeColumn(onlineSelfPlatformInsertList, hotelProductChannelOnlineSelfPlatformMapper);
        insertBatchSomeColumn(onlineThirdPlatformInsertList, hotelProductChannelOnlineThirdPlatformMapper);
        return Result.success();
    }

    /** 修改方法 */
    private void updateBatchSomeColumn(List list, BaseMapper mapper) {
        if (CollUtil.isNotEmpty(list)) {
            for (Object o : list) {
                mapper.updateById(o);
            }
        }
    }


    /** 新增方法 */
    private void insertBatchSomeColumn(List list, BaseMapper mapper) {
        if (CollUtil.isNotEmpty(list)) {
            for (Object o : list) {
                mapper.insert(o);
            }
        }
    }

    /** 酒店产品批量加幅保存 */
    @Override
    public Result saveBatch(HotelProductStrategySaveBatchReq hotelProductStrategySaveBatchReq, Long userId) {
        List<Long> hotelProductIdList = hotelProductStrategySaveBatchReq.getHotelProductIdList();
        List<HotelProductStrategyReq> hotelProductStrategyReqList = hotelProductStrategySaveBatchReq.getHotelProductStrategyReqList();
        // 校验
        Result checkResult = checkStrategyDateResult(hotelProductStrategyReqList);
        if (null != checkResult) {
            return checkResult;
        }
        LocalDateTime nowDateTime = LocalDateTime.now();
        // 删除加幅策略
        deleteStrategyByHotelProductId(hotelProductIdList);
        // 插入加幅策略
        List<HotelProductStrategy> hotelProductStrategyList = new ArrayList<>();
        List<HotelProductIncreaseStrategyRule> hotelProductIncreaseStrategyRuleList = new ArrayList<>();
        List<HotelProductStrategyDate> hotelProductStrategyDateList = new ArrayList<>();
        List<HotelProductChannelOfflineAgentCustom> hotelProductChannelOfflineAgentCustomList = new ArrayList<>();
        List<HotelProductChannelOfflineSelfCustom> hotelProductChannelOfflineSelfCustomList = new ArrayList<>();
        List<HotelProductChannelOnlineSelfPlatform> hotelProductChannelOnlineSelfPlatformList = new ArrayList<>();
        List<HotelProductChannelOnlineThirdPlatform> hotelProductChannelOnlineThirdPlatformList = new ArrayList<>();
        for (Long hotelProductId : hotelProductIdList) {
            for (HotelProductStrategyReq increaseStrategyReq : hotelProductStrategyReqList) {
                // 构建加幅策略
                HotelProductStrategy increaseStrategy = EntityUtil.copy(increaseStrategyReq, HotelProductStrategy.class);
                increaseStrategy.setHotelProductId(hotelProductId);
                increaseStrategy.setId(SnowflakeIdWorker.nextId());
                increaseStrategy.setCreateId(userId);
                increaseStrategy.setGmtCreate(nowDateTime);
                increaseStrategy.setGmtModified(nowDateTime);
                increaseStrategy.setIsDeleted(0);
                // 构建加幅策略日期段
                List<HotelProductStrategyDate> increaseStrategyDateList = EntityUtil.copyList(increaseStrategyReq.getHotelProductStrategyDateReqList(), HotelProductStrategyDate.class);
                for (HotelProductStrategyDate strategyDate : increaseStrategyDateList) {
                    strategyDate.setId(SnowflakeIdWorker.nextId());
                    strategyDate.setIncreaseStrategyId(increaseStrategy.getId());
                    strategyDate.setGmtCreate(nowDateTime);
                    strategyDate.setGmtModified(nowDateTime);
                }
                hotelProductStrategyDateList.addAll(increaseStrategyDateList);
                // 构建加幅策略-线下自营规则-所有
                if (SALE_CHANNEL_TYPE_ALL.equals(increaseStrategy.getSelfSalePermissionType())) {
                    HotelProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(increaseStrategyReq.getHotelProductIncreaseStrategySelfRule(), HotelProductIncreaseStrategyRule.class);
                    increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                    increaseStrategyRule.setIncreaseStrategyId(increaseStrategy.getId());
                    increaseStrategyRule.setSoldNumber(null);
                    increaseStrategy.setSelfSalePermissionId(increaseStrategyRule.getId());
                    hotelProductIncreaseStrategyRuleList.add(increaseStrategyRule);
                }
                // 构建加幅策略-线下自营规则-指定
                if (SALE_CHANNEL_TYPE_SELECTED.equals(increaseStrategyReq.getSelfSalePermissionType()) && CollUtil.isNotEmpty(increaseStrategyReq.getHotelProductChannelOfflineSelfCustomReqList())) {
                    increaseStrategyReq.getHotelProductChannelOfflineSelfCustomReqList().forEach(channelOfflineSelfCustomReq -> {
                        HotelProductChannelOfflineSelfCustom channelOfflineSelfCustom = EntityUtil.copy(channelOfflineSelfCustomReq, HotelProductChannelOfflineSelfCustom.class);
                        channelOfflineSelfCustom.setId(SnowflakeIdWorker.nextId());
                        channelOfflineSelfCustom.setIncreaseStrategyId(increaseStrategy.getId());
                        channelOfflineSelfCustom.setRelationId(channelOfflineSelfCustomReq.getRelationId());
                        channelOfflineSelfCustom.setCreateId(userId);
                        channelOfflineSelfCustom.setGmtCreate(nowDateTime);
                        channelOfflineSelfCustom.setGmtModified(nowDateTime);
                        channelOfflineSelfCustom.setIsDeleted(IsDeletedCode.NO.getCode());
                        hotelProductChannelOfflineSelfCustomList.add(channelOfflineSelfCustom);
                        // 插入加幅规则
                        HotelProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOfflineSelfCustomReq.getHotelProductIncreaseStrategyRuleReq(), HotelProductIncreaseStrategyRule.class);
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        increaseStrategyRule.setIncreaseStrategyId(increaseStrategy.getId());
                        increaseStrategyRule.setCustomId(channelOfflineSelfCustom.getId());
                        increaseStrategyRule.setSoldNumber(null);
                        hotelProductIncreaseStrategyRuleList.add(increaseStrategyRule);

                        increaseStrategy.setSelfSalePermissionId(increaseStrategyRule.getId());
                    });
                }
                // 构建加幅策略-线下代理规则-所有
                if (SALE_CHANNEL_TYPE_ALL.equals(increaseStrategy.getAgentSalePermissionType())) {
                    HotelProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(increaseStrategyReq.getHotelProductIncreaseStrategyAgentRule(), HotelProductIncreaseStrategyRule.class);
                    increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                    increaseStrategyRule.setIncreaseStrategyId(increaseStrategy.getId());
                    increaseStrategyRule.setSoldNumber(null);
                    increaseStrategy.setAgentSalePermissionId(increaseStrategyRule.getId());
                    hotelProductIncreaseStrategyRuleList.add(increaseStrategyRule);
                }
                // 构建加幅策略-线下代理规则-指定
                if (SALE_CHANNEL_TYPE_SELECTED.equals(increaseStrategyReq.getAgentSalePermissionType()) && CollUtil.isNotEmpty(increaseStrategyReq.getHotelProductChannelOfflineAgentCustomReqList())) {
                    for (HotelProductChannelOfflineAgentCustomReq channelOfflineAgentCustomReq : increaseStrategyReq.getHotelProductChannelOfflineAgentCustomReqList()) {
                        HotelProductChannelOfflineAgentCustom channelOfflineAgentCustom = EntityUtil.copy(channelOfflineAgentCustomReq, HotelProductChannelOfflineAgentCustom.class);
                        channelOfflineAgentCustom.setId(SnowflakeIdWorker.nextId());
                        channelOfflineAgentCustom.setIncreaseStrategyId(increaseStrategy.getId());
                        channelOfflineAgentCustom.setCreateId(userId);
                        channelOfflineAgentCustom.setGmtCreate(nowDateTime);
                        channelOfflineAgentCustom.setGmtModified(nowDateTime);
                        channelOfflineAgentCustom.setIsDeleted(IsDeletedCode.NO.getCode());
                        hotelProductChannelOfflineAgentCustomList.add(channelOfflineAgentCustom);
                        // 插入加幅规则
                        HotelProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOfflineAgentCustomReq.getHotelProductIncreaseStrategyRuleReq(), HotelProductIncreaseStrategyRule.class);
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        increaseStrategyRule.setIncreaseStrategyId(increaseStrategy.getId());
                        increaseStrategyRule.setCustomId(channelOfflineAgentCustom.getId());
                        increaseStrategyRule.setSoldNumber(null);
                        hotelProductIncreaseStrategyRuleList.add(increaseStrategyRule);
                    }
                }
                // 线上自营
                if (CollUtil.isNotEmpty(increaseStrategyReq.getHotelProductChannelOnlineSelfPlatformReqList())) {
                    increaseStrategyReq.getHotelProductChannelOnlineSelfPlatformReqList().forEach(channelOnlineSelfPlatformReq -> {
                        HotelProductChannelOnlineSelfPlatform channelOnlineSelfPlatform = EntityUtil.copy(channelOnlineSelfPlatformReq, HotelProductChannelOnlineSelfPlatform.class);
                        channelOnlineSelfPlatform.setId(SnowflakeIdWorker.nextId());
                        channelOnlineSelfPlatform.setIncreaseStrategyId(increaseStrategy.getId());
                        channelOnlineSelfPlatform.setCreateId(userId);
                        channelOnlineSelfPlatform.setGmtCreate(nowDateTime);
                        channelOnlineSelfPlatform.setGmtModified(nowDateTime);
                        channelOnlineSelfPlatform.setIsDeleted(IsDeletedCode.NO.getCode());
                        hotelProductChannelOnlineSelfPlatformList.add(channelOnlineSelfPlatform);
                        // 插入加幅规则
                        HotelProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOnlineSelfPlatformReq.getHotelProductIncreaseStrategyRuleReq(), HotelProductIncreaseStrategyRule.class);
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        increaseStrategyRule.setIncreaseStrategyId(increaseStrategy.getId());
                        increaseStrategyRule.setCustomId(channelOnlineSelfPlatform.getId());
                        increaseStrategyRule.setSoldNumber(null);
                        hotelProductIncreaseStrategyRuleList.add(increaseStrategyRule);
                    });
                }
                // 线上第三方
                if (CollUtil.isNotEmpty(increaseStrategyReq.getHotelProductChannelOnlineThirdPlatformReqList())) {
                    increaseStrategyReq.getHotelProductChannelOnlineThirdPlatformReqList().forEach(channelOnlineThirdPlatformReq -> {
                        HotelProductChannelOnlineThirdPlatform channelOnlineSelfPlatform = EntityUtil.copy(channelOnlineThirdPlatformReq, HotelProductChannelOnlineThirdPlatform.class);
                        channelOnlineSelfPlatform.setId(SnowflakeIdWorker.nextId());
                        channelOnlineSelfPlatform.setIncreaseStrategyId(increaseStrategy.getId());
                        channelOnlineSelfPlatform.setCreateId(userId);
                        channelOnlineSelfPlatform.setGmtCreate(nowDateTime);
                        channelOnlineSelfPlatform.setGmtModified(nowDateTime);
                        channelOnlineSelfPlatform.setIsDeleted(IsDeletedCode.NO.getCode());
                        hotelProductChannelOnlineThirdPlatformList.add(channelOnlineSelfPlatform);
                        // 插入加幅规则
                        HotelProductIncreaseStrategyRule increaseStrategyRule = EntityUtil.copy(channelOnlineThirdPlatformReq.getHotelProductIncreaseStrategyRuleReq(), HotelProductIncreaseStrategyRule.class);
                        increaseStrategyRule.setId(SnowflakeIdWorker.nextId());
                        increaseStrategyRule.setIncreaseStrategyId(increaseStrategy.getId());
                        increaseStrategyRule.setCustomId(channelOnlineSelfPlatform.getId());
                        increaseStrategyRule.setSoldNumber(null);
                        hotelProductIncreaseStrategyRuleList.add(increaseStrategyRule);
                    });
                }
                hotelProductStrategyList.add(increaseStrategy);
            }
        }
        insertBatchSomeColumn(hotelProductStrategyList, hotelProductStrategyMapper);
        insertBatchSomeColumn(hotelProductStrategyDateList, hotelProductStrategyDateMapper);
        insertBatchSomeColumn(hotelProductIncreaseStrategyRuleList, hotelProductIncreaseStrategyRuleMapper);
        insertBatchSomeColumn(hotelProductChannelOfflineAgentCustomList, hotelProductChannelOfflineAgentCustomMapper);
        insertBatchSomeColumn(hotelProductChannelOfflineSelfCustomList, hotelProductChannelOfflineSelfCustomMapper);
        insertBatchSomeColumn(hotelProductChannelOnlineSelfPlatformList, hotelProductChannelOnlineSelfPlatformMapper);
        insertBatchSomeColumn(hotelProductChannelOnlineThirdPlatformList, hotelProductChannelOnlineThirdPlatformMapper);

        Optional.ofNullable(hotelProductIdList).orElse(new ArrayList<>()).forEach(hotelPrudctId -> {
            HotelProduct hotelProduct = new HotelProduct();
            hotelProduct.setModifiedId(userId);
            hotelProduct.setOnShelfStatus(SaleOnShelfStatusCode.ON_SHELF_STATUS_1.getCode());
            hotelProduct.setGmtModified(nowDateTime);
            hotelProductMapper.update(hotelProduct, Wrappers.<HotelProduct>lambdaQuery().eq(HotelProduct::getId, hotelPrudctId));
        });

        /**
         * 清除C端缓存
         */
        ThreadUtil.execAsync(() -> {
            Optional.ofNullable(hotelProductIdList).orElse(new ArrayList<>()).forEach(hotelPrudctId -> {
                HotelProduct hotelProduct = hotelProductMapper.selectById(hotelPrudctId);
                hotelH5Service.cacheUpdate(hotelProduct.getHotelId());
            });
        });

        return Result.success(hotelProductIdList.get(0));
    }

    /**
     * 根据酒店产品id删除策略
     *
     * @param hotelProductIds 酒店产品id列表
     */
    private void deleteStrategyByHotelProductId(List<Long> hotelProductIds) {
        if (CollUtil.isNotEmpty(hotelProductIds)) {
            List<Long> increaseStrategyIds = hotelProductStrategyMapper.selectIdListByHotelProductIds(hotelProductIds);
            if (CollUtil.isNotEmpty(increaseStrategyIds)) {
                hotelProductChannelOfflineSelfCustomMapper.delete(new QueryWrapper<HotelProductChannelOfflineSelfCustom>().lambda().in(HotelProductChannelOfflineSelfCustom::getIncreaseStrategyId, increaseStrategyIds));
                hotelProductChannelOfflineAgentCustomMapper.delete(new QueryWrapper<HotelProductChannelOfflineAgentCustom>().lambda().in(HotelProductChannelOfflineAgentCustom::getIncreaseStrategyId, increaseStrategyIds));
                hotelProductChannelOnlineSelfPlatformMapper.delete(new QueryWrapper<HotelProductChannelOnlineSelfPlatform>().lambda().in(HotelProductChannelOnlineSelfPlatform::getIncreaseStrategyId, increaseStrategyIds));
                hotelProductChannelOnlineThirdPlatformMapper.delete(new QueryWrapper<HotelProductChannelOnlineThirdPlatform>().lambda().in(HotelProductChannelOnlineThirdPlatform::getIncreaseStrategyId, increaseStrategyIds));
                hotelProductIncreaseStrategyRuleMapper.delete(new QueryWrapper<HotelProductIncreaseStrategyRule>().lambda().in(HotelProductIncreaseStrategyRule::getIncreaseStrategyId, increaseStrategyIds));
                hotelProductStrategyDateMapper.delete(new QueryWrapper<HotelProductStrategyDate>().lambda().in(HotelProductStrategyDate::getIncreaseStrategyId, increaseStrategyIds));
                hotelProductStrategyMapper.delete(new QueryWrapper<HotelProductStrategy>().lambda().in(HotelProductStrategy::getId, increaseStrategyIds));
            }
        }
    }

    /**
     * 查询产品加幅信息
     * @param productId
     * @return
     */
    @Override
    public List<HotelProductStrategyDTO> queryByProductId(Long productId) {

        // 查询加幅策略列表
        QueryWrapper<HotelProductStrategy> aeIncreaseStrategyWrapper = new QueryWrapper<>();
        aeIncreaseStrategyWrapper.lambda().eq(HotelProductStrategy::getHotelProductId, productId);
        aeIncreaseStrategyWrapper.lambda().eq(HotelProductStrategy::getIsDeleted, IsDeletedCode.NO.getCode());
        List<HotelProductStrategy> hotelProductStrategyList = hotelProductStrategyMapper.selectList(aeIncreaseStrategyWrapper);
        List<HotelProductStrategyDTO> hotelProductStrategyDTOList = EntityUtil.copyList(hotelProductStrategyList, HotelProductStrategyDTO.class);
        for (HotelProductStrategyDTO aeIncreaseStrategyDTO : hotelProductStrategyDTOList) {
            // 查询加幅策略-日期列表
            QueryWrapper<HotelProductStrategyDate> aeIncreaseStrategyDateWrapper = new QueryWrapper<>();
            aeIncreaseStrategyDateWrapper.eq("increase_strategy_id", aeIncreaseStrategyDTO.getId());
            List<HotelProductStrategyDate> aeIncreaseStrategyDates = hotelProductStrategyDateMapper.selectList(aeIncreaseStrategyDateWrapper);
            aeIncreaseStrategyDTO.setHotelProductStrategyDateDTOList(EntityUtil.copyList(aeIncreaseStrategyDates, HotelProductStrategyDateDTO.class));
            // 查询加幅策略-规则列表
            QueryWrapper<HotelProductIncreaseStrategyRule> aeIncreaseStrategyRuleWrapper = new QueryWrapper<>();
            aeIncreaseStrategyRuleWrapper.eq("increase_strategy_id", aeIncreaseStrategyDTO.getId());
            List<HotelProductIncreaseStrategyRule> aeIncreaseStrategyRules = hotelProductIncreaseStrategyRuleMapper.selectList(aeIncreaseStrategyRuleWrapper);
            List<HotelProductIncreaseStrategyRuleDTO> aeIncreaseStrategyRuleDTOs = EntityUtil.copyList(aeIncreaseStrategyRules, HotelProductIncreaseStrategyRuleDTO.class);
            if (SALE_CHANNEL_TYPE_ALL.equals(aeIncreaseStrategyDTO.getSelfSalePermissionType())) {
                List<HotelProductIncreaseStrategyRuleDTO> ruleList = aeIncreaseStrategyRuleDTOs.stream().filter(ruleDTO -> aeIncreaseStrategyDTO.getSelfSalePermissionId().equals(ruleDTO.getId())).collect(Collectors.toList());
                aeIncreaseStrategyDTO.setHotelIncreaseStrategySelfRule(ruleList.get(0));
                aeIncreaseStrategyRuleDTOs.remove(ruleList.get(0));
            }
            if (SALE_CHANNEL_TYPE_ALL.equals(aeIncreaseStrategyDTO.getAgentSalePermissionType())) {
                List<HotelProductIncreaseStrategyRuleDTO> ruleList = aeIncreaseStrategyRuleDTOs.stream().filter(ruleDTO -> aeIncreaseStrategyDTO.getAgentSalePermissionId().equals(ruleDTO.getId())).collect(Collectors.toList());
                aeIncreaseStrategyDTO.setHotelIncreaseStrategyAgentRule(ruleList.get(0));
                aeIncreaseStrategyRuleDTOs.remove(ruleList.get(0));
            }
            // 查询加幅策略-线下自营
            if (SALE_CHANNEL_TYPE_SELECTED.equals(aeIncreaseStrategyDTO.getSelfSalePermissionType())) {
                QueryWrapper<HotelProductChannelOfflineSelfCustom> aeChannelOfflineSelfCustomWrapper = new QueryWrapper<>();
                aeChannelOfflineSelfCustomWrapper.eq("increase_strategy_id", aeIncreaseStrategyDTO.getId());
                aeChannelOfflineSelfCustomWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
                List<HotelProductChannelOfflineSelfCustom> aeChannelOfflineSelfCustoms = hotelProductChannelOfflineSelfCustomMapper.selectList(aeChannelOfflineSelfCustomWrapper);
                List<HotelProductChannelOfflineSelfCustomDTO> aeChannelOfflineSelfCustomDTOs = EntityUtil.copyList(aeChannelOfflineSelfCustoms, HotelProductChannelOfflineSelfCustomDTO.class);
                // 组装所属加幅规则
                for (HotelProductChannelOfflineSelfCustomDTO aeChannelOfflineSelfCustomDTO : aeChannelOfflineSelfCustomDTOs) {
                    List<HotelProductIncreaseStrategyRuleDTO> ruleList = aeIncreaseStrategyRuleDTOs.stream().filter(ruleDTO -> ruleDTO.getCustomId().equals(aeChannelOfflineSelfCustomDTO.getId())).collect(Collectors.toList());
                    aeChannelOfflineSelfCustomDTO.setHotelProductIncreaseStrategyRuleDTO(ruleList.get(0));
                }
                aeIncreaseStrategyDTO.setHotelProductChannelOfflineSelfCustomDTOList(aeChannelOfflineSelfCustomDTOs);
            }
            // 查询加幅策略-线下代理
            if (SALE_CHANNEL_TYPE_SELECTED.equals(aeIncreaseStrategyDTO.getAgentSalePermissionType())) {
                QueryWrapper<HotelProductChannelOfflineAgentCustom> aeChannelOfflineAgentCustomWrapper = new QueryWrapper<>();
                aeChannelOfflineAgentCustomWrapper.eq("increase_strategy_id", aeIncreaseStrategyDTO.getId());
                aeChannelOfflineAgentCustomWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
                List<HotelProductChannelOfflineAgentCustom> aeChannelOfflineAgentCustoms = hotelProductChannelOfflineAgentCustomMapper.selectList(aeChannelOfflineAgentCustomWrapper);
                List<HotelProductChannelOfflineAgentCustomDTO> aeChannelOfflineAgentCustomDTOs = EntityUtil.copyList(aeChannelOfflineAgentCustoms, HotelProductChannelOfflineAgentCustomDTO.class);
                // 组装所属加幅规则
                for (HotelProductChannelOfflineAgentCustomDTO aeChannelOfflineAgentCustomDTO : aeChannelOfflineAgentCustomDTOs) {
                    List<HotelProductIncreaseStrategyRuleDTO> ruleList = aeIncreaseStrategyRuleDTOs.stream().filter(ruleDTO -> ruleDTO.getCustomId().equals(aeChannelOfflineAgentCustomDTO.getId())).collect(Collectors.toList());
                    aeChannelOfflineAgentCustomDTO.setHotelProductIncreaseStrategyRuleDTO(ruleList.get(0));
                }
                aeIncreaseStrategyDTO.setHotelProductChannelOfflineAgentCustomDTOList(aeChannelOfflineAgentCustomDTOs);
            }
            // 查询加幅策略-线上自营
            QueryWrapper<HotelProductChannelOnlineSelfPlatform> aeIncreaseChannelOnlineSelfPlatformWrapper = new QueryWrapper<>();
            aeIncreaseChannelOnlineSelfPlatformWrapper.eq("increase_strategy_id", aeIncreaseStrategyDTO.getId());
            aeIncreaseChannelOnlineSelfPlatformWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
            List<HotelProductChannelOnlineSelfPlatform> aeChannelOnlineSelfPlatforms = hotelProductChannelOnlineSelfPlatformMapper.selectList(aeIncreaseChannelOnlineSelfPlatformWrapper);
            List<HotelProductChannelOnlineSelfPlatformDTO> aeChannelOnlineSelfPlatformDTOs = EntityUtil.copyList(aeChannelOnlineSelfPlatforms, HotelProductChannelOnlineSelfPlatformDTO.class);
            // 组装所属加幅规则
            for (HotelProductChannelOnlineSelfPlatformDTO aeChannelOnlineSelfPlatformDTO : aeChannelOnlineSelfPlatformDTOs) {
                List<HotelProductIncreaseStrategyRuleDTO> ruleList = aeIncreaseStrategyRuleDTOs.stream().filter(ruleDTO -> ruleDTO.getCustomId().equals(aeChannelOnlineSelfPlatformDTO.getId())).collect(Collectors.toList());
                aeChannelOnlineSelfPlatformDTO.setHotelProductIncreaseStrategyRuleDTO(ruleList.get(0));
            }
            aeIncreaseStrategyDTO.setHotelProductChannelOnlineSelfPlatformDTOList(aeChannelOnlineSelfPlatformDTOs);
            // 查询加幅策略-线上第三方
            QueryWrapper<HotelProductChannelOnlineThirdPlatform> aeChannelOnlineThirdWrapper = new QueryWrapper<>();
            aeChannelOnlineThirdWrapper.eq("increase_strategy_id", aeIncreaseStrategyDTO.getId());
            aeChannelOnlineThirdWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
            List<HotelProductChannelOnlineThirdPlatform> aeChannelOnlineThirds = hotelProductChannelOnlineThirdPlatformMapper.selectList(aeChannelOnlineThirdWrapper);
            List<HotelProductChannelOnlineThirdPlatformDTO> aeChannelOnlineThirdPlatformDTOs = EntityUtil.copyList(aeChannelOnlineThirds, HotelProductChannelOnlineThirdPlatformDTO.class);
            // 组装所属加幅规则
            for (HotelProductChannelOnlineThirdPlatformDTO aeChannelOnlineThirdPlatformDTO : aeChannelOnlineThirdPlatformDTOs) {
                List<HotelProductIncreaseStrategyRuleDTO> ruleList = aeIncreaseStrategyRuleDTOs.stream().filter(ruleDTO -> ruleDTO.getCustomId().equals(aeChannelOnlineThirdPlatformDTO.getId())).collect(Collectors.toList());
                aeChannelOnlineThirdPlatformDTO.setHotelProductIncreaseStrategyRuleDTO(ruleList.get(0));
            }
            aeIncreaseStrategyDTO.setHotelProductChannelOnlineThirdPlatformDTOList(aeChannelOnlineThirdPlatformDTOs);
        }
        return hotelProductStrategyDTOList;
    }

    /**
     * 产品中心-查看详情
     * @param hotelProductStrategysDateReq
     * @return
     */
    @Override
    public Result<HotelProductDetailDTO> selectDetail(HotelProductStrategysDateReq hotelProductStrategysDateReq) {
        if(hotelProductStrategysDateReq==null){
            return Result.failed(SystemError.HOTEL_10068);
        }else if(hotelProductStrategysDateReq.getProductId()==null){
            return Result.failed(SystemError.HOTEL_10068);
        }else if(hotelProductStrategysDateReq.getYearAndMonth()==null){
            return Result.failed(SystemError.HOTEL_10068);
        }

        HotelProduct hotelProduct = hotelProductMapper.selectById(hotelProductStrategysDateReq.getProductId());
        HotelProductDetailDTO hotelProductDetailDTO = EntityUtil.copy(hotelProduct, HotelProductDetailDTO.class);
        //查询酒店信息
        Hotel hotel = hotelMapper.selectById(hotelProductDetailDTO.getHotelId());
        if(hotel==null){
            Result.failed(SystemError.HOTEL_10066);
        }
        hotelProductDetailDTO.setBrandName(hotel.getBrandName());
        hotelProductDetailDTO.setHotelName(hotel.getName());
        //查询房型名称
        HotelRoomType hotelRoomType = hotelRoomTypeMapper.selectById(hotelProductDetailDTO.getRoomId());
        if(hotelRoomType==null){
            Result.failed(SystemError.HOTEL_10067);
        }
        hotelProductDetailDTO.setRoomName(hotelRoomType.getName());
        // 查询加幅策略列表
        QueryWrapper<HotelProductStrategy> strategyQueryWrapper = new QueryWrapper<>();
        strategyQueryWrapper.eq("hotel_product_id", hotelProductStrategysDateReq.getProductId());
        List<HotelProductStrategy> hotelProductStrategyList = hotelProductStrategyMapper.selectList(strategyQueryWrapper);
//        TreeMap<LocalDate, HotelProductIncreaseStrategyRuleDTO> dateRuleMap = buildHotelProductList(hotelProductStrategyList,hotelProductStrategysDateReq.getYearAndMonth());
        TreeMap<LocalDate, HotelDetailDTO> dateRuleMap = buildHotelProductList(hotelProductStrategyList, hotelProductStrategysDateReq.getYearAndMonth());
        hotelProductDetailDTO.setDateRuleInfoMap(dateRuleMap);
        return Result.success(hotelProductDetailDTO);
    }
    /**
     * 组装每天的加幅策略表
     * @return
     * @param strategyList
     */
    private TreeMap<LocalDate, HotelDetailDTO> buildHotelProductList(List<HotelProductStrategy> strategyList,String yearAndMonth) {
        HashMap<LocalDate, HotelDetailDTO> localDateRuleInfoMap = new HashMap<>();
        HashMap<LocalDate, HotelDetailDTO> localDateRuleInfoMap2 = new HashMap<>();
        for (HotelProductStrategy hotelProductStrategy : strategyList) {
            List<HotelProductIncreaseStrategyRuleDTO> offlineRuleList = new ArrayList<>();
            List<HotelProductIncreaseStrategyRuleDTO> onlineRuleList = new ArrayList<>();
            // 查询当前加幅策略-所有规则列表
            QueryWrapper<HotelProductIncreaseStrategyRule> strategyRuleQueryWrapper = new QueryWrapper<>();
            strategyRuleQueryWrapper.eq("increase_strategy_id", hotelProductStrategy.getId());
            List<HotelProductIncreaseStrategyRule> strategyRuleList = hotelProductIncreaseStrategyRuleMapper.selectList(strategyRuleQueryWrapper);
            // 加幅策略-所有线下自营
            if (SALE_CHANNEL_TYPE_ALL == hotelProductStrategy.getSelfSalePermissionType()) {
                List<HotelProductIncreaseStrategyRule> ruleList = strategyRuleList.stream().filter(rule -> hotelProductStrategy.getSelfSalePermissionId().equals(rule.getId())).collect(Collectors.toList());
                HotelProductIncreaseStrategyRuleDTO strategyRuleInfoDTO = EntityUtil.copy(ruleList.get(0), HotelProductIncreaseStrategyRuleDTO.class);
                strategyRuleInfoDTO.setChannelFullName("线下自营渠道-所有");
                if (strategyRuleInfoDTO.getSaleableType() == SALE_TYPE_LIMIT) {
                    int soldNumber = strategyRuleInfoDTO.getSoldNumber() == null ? 0 : strategyRuleInfoDTO.getSoldNumber();
                    strategyRuleInfoDTO.setRemainderNumber(strategyRuleInfoDTO.getSaleableNumber() - soldNumber);
                }
                offlineRuleList.add(strategyRuleInfoDTO);
            }
            // 加幅策略-所有线下代理
            if (SALE_CHANNEL_TYPE_ALL == hotelProductStrategy.getAgentSalePermissionType()) {
                List<HotelProductIncreaseStrategyRule> ruleList = strategyRuleList.stream().filter(rule -> hotelProductStrategy.getAgentSalePermissionId().equals(rule.getId())).collect(Collectors.toList());
                HotelProductIncreaseStrategyRuleDTO strategyRuleInfoDTO = EntityUtil.copy(ruleList.get(0), HotelProductIncreaseStrategyRuleDTO.class);
                strategyRuleInfoDTO.setChannelFullName("线下代理渠道-所有");
                if (strategyRuleInfoDTO.getSaleableType() == SALE_TYPE_LIMIT) {
                    int soldNumber = strategyRuleInfoDTO.getSoldNumber() == null ? 0 : strategyRuleInfoDTO.getSoldNumber();
                    strategyRuleInfoDTO.setRemainderNumber(strategyRuleInfoDTO.getSaleableNumber() - soldNumber);
                }
                offlineRuleList.add(strategyRuleInfoDTO);
            }
            // 查询加幅策略-选择线下自营
            if (SALE_CHANNEL_TYPE_SELECTED == hotelProductStrategy.getSelfSalePermissionType()) {
                QueryWrapper<HotelProductChannelOfflineSelfCustom> selfCustomQueryWrapper = new QueryWrapper<>();
                selfCustomQueryWrapper.eq("increase_strategy_id", hotelProductStrategy.getId());
                selfCustomQueryWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
                List<HotelProductChannelOfflineSelfCustom> offlineSelfCustomList = hotelProductChannelOfflineSelfCustomMapper.selectList(selfCustomQueryWrapper);
                // 组装所属加幅规则
                for (HotelProductChannelOfflineSelfCustom offlineSelfCustom : offlineSelfCustomList) {
                    List<HotelProductIncreaseStrategyRule> ruleList = strategyRuleList.stream().filter(rule -> rule.getCustomId().equals(offlineSelfCustom.getId())).collect(Collectors.toList());
                    if(CollUtil.isNotEmpty(ruleList)){
                    HotelProductIncreaseStrategyRuleDTO strategyRuleInfoDTO = EntityUtil.copy(ruleList.get(0), HotelProductIncreaseStrategyRuleDTO.class);
                    strategyRuleInfoDTO.setChannelFullName("线下自营渠道-" + offlineSelfCustom.getName());
                    if (strategyRuleInfoDTO.getSaleableType() == SALE_TYPE_LIMIT) {
                        int soldNumber = strategyRuleInfoDTO.getSoldNumber() == null ? 0 : strategyRuleInfoDTO.getSoldNumber();
                        strategyRuleInfoDTO.setRemainderNumber(strategyRuleInfoDTO.getSaleableNumber() - soldNumber);
                    }
                        offlineRuleList.add(strategyRuleInfoDTO);
                    }
                }
            }
            // 查询加幅策略-选择线下代理
            if (SALE_CHANNEL_TYPE_SELECTED == hotelProductStrategy.getAgentSalePermissionType()) {
                QueryWrapper<HotelProductChannelOfflineAgentCustom> agentCustomQueryWrapper = new QueryWrapper<>();
                agentCustomQueryWrapper.eq("increase_strategy_id", hotelProductStrategy.getId());
                agentCustomQueryWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
                List<HotelProductChannelOfflineAgentCustom> offlineAgentCustomList = hotelProductChannelOfflineAgentCustomMapper.selectList(agentCustomQueryWrapper);
                // 组装所属加幅规则
                for (HotelProductChannelOfflineAgentCustom offlineAgentCustom : offlineAgentCustomList) {
                    List<HotelProductIncreaseStrategyRule> ruleList = strategyRuleList.stream().filter(rule -> rule.getCustomId().equals(offlineAgentCustom.getId())).collect(Collectors.toList());
                    if(CollUtil.isNotEmpty(ruleList)){
                        HotelProductIncreaseStrategyRuleDTO strategyRuleInfoDTO = EntityUtil.copy(ruleList.get(0), HotelProductIncreaseStrategyRuleDTO.class);
                        strategyRuleInfoDTO.setChannelFullName("线下代理渠道-" + offlineAgentCustom.getAgentName());
                        if (strategyRuleInfoDTO.getSaleableType() == SALE_TYPE_LIMIT) {
                            int soldNumber = strategyRuleInfoDTO.getSoldNumber() == null ? 0 : strategyRuleInfoDTO.getSoldNumber();
                            strategyRuleInfoDTO.setRemainderNumber(strategyRuleInfoDTO.getSaleableNumber() - soldNumber);
                        }
                        offlineRuleList.add(strategyRuleInfoDTO);
                    }
                }
            }
            // 查询加幅策略-线上自营
            QueryWrapper<HotelProductChannelOnlineSelfPlatform> onlineSelfPlatformQueryWrapper = new QueryWrapper<>();
            onlineSelfPlatformQueryWrapper.eq("increase_strategy_id", hotelProductStrategy.getId());
            onlineSelfPlatformQueryWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
            List<HotelProductChannelOnlineSelfPlatform> onlineSelfPlatformList = hotelProductChannelOnlineSelfPlatformMapper.selectList(onlineSelfPlatformQueryWrapper);
            // 查询加幅策略-线上自营-组装所属加幅规则
            for (HotelProductChannelOnlineSelfPlatform onlineSelfPlatform : onlineSelfPlatformList) {
                List<HotelProductIncreaseStrategyRule> ruleList = strategyRuleList.stream().filter(rule -> rule.getCustomId().equals(onlineSelfPlatform.getId())).collect(Collectors.toList());
                if(CollUtil.isNotEmpty(ruleList)){
                    HotelProductIncreaseStrategyRuleDTO strategyRuleInfoDTO = EntityUtil.copy(ruleList.get(0), HotelProductIncreaseStrategyRuleDTO.class);
                    strategyRuleInfoDTO.setChannelFullName("线上自营渠道-" + onlineSelfPlatform.getChannelName());
                    if (strategyRuleInfoDTO.getSaleableType() == SALE_TYPE_LIMIT) {
                        int soldNumber = strategyRuleInfoDTO.getSoldNumber() == null ? 0 : strategyRuleInfoDTO.getSoldNumber();
                        strategyRuleInfoDTO.setRemainderNumber(strategyRuleInfoDTO.getSaleableNumber() - soldNumber);
                    }
                    onlineRuleList.add(strategyRuleInfoDTO);
                }
            }
            // 查询加幅策略-线上第三方
            QueryWrapper<HotelProductChannelOnlineThirdPlatform> onlineThirdPlatformQueryWrapper = new QueryWrapper<>();
            onlineThirdPlatformQueryWrapper.eq("increase_strategy_id", hotelProductStrategy.getId());
            onlineThirdPlatformQueryWrapper.eq("is_deleted", IsDeletedCode.NO.getCode());
            List<HotelProductChannelOnlineThirdPlatform> onlineThirdPlatformList = hotelProductChannelOnlineThirdPlatformMapper.selectList(onlineThirdPlatformQueryWrapper);
            // 查询加幅策略-线上第三方-组装所属加幅规则
            for (HotelProductChannelOnlineThirdPlatform onlineThirdPlatform : onlineThirdPlatformList) {
                List<HotelProductIncreaseStrategyRule> ruleList = strategyRuleList.stream().filter(rule -> rule.getCustomId().equals(onlineThirdPlatform.getId())).collect(Collectors.toList());
                if(CollUtil.isNotEmpty(ruleList)){
                    HotelProductIncreaseStrategyRuleDTO strategyRuleInfoDTO = EntityUtil.copy(ruleList.get(0), HotelProductIncreaseStrategyRuleDTO.class);
                    strategyRuleInfoDTO.setChannelFullName("线上代理渠道-" + onlineThirdPlatform.getPlatformName());
                    if (strategyRuleInfoDTO.getSaleableType() == SALE_TYPE_LIMIT) {
                        int soldNumber = strategyRuleInfoDTO.getSoldNumber() == null ? 0 : strategyRuleInfoDTO.getSoldNumber();
                        strategyRuleInfoDTO.setRemainderNumber(strategyRuleInfoDTO.getSaleableNumber() - soldNumber);
                    }
                    onlineRuleList.add(strategyRuleInfoDTO);
                }
            }

            HotelDetailDTO airportExpressDetailDTO = new HotelDetailDTO(offlineRuleList, onlineRuleList);
            // 查询加幅策略-日期列表
            QueryWrapper<HotelProductStrategyDate> aeIncreaseStrategyDateWrapper = new QueryWrapper<>();
            aeIncreaseStrategyDateWrapper.eq("increase_strategy_id", hotelProductStrategy.getId());
            List<HotelProductStrategyDate> hotelProductStrategyDateList = hotelProductStrategyDateMapper.selectList(aeIncreaseStrategyDateWrapper);
            // 组装日期map
            for (HotelProductStrategyDate hotelProductStrategyDate : hotelProductStrategyDateList) {
                long diffDay = hotelProductStrategyDate.getDateEnd().toEpochDay() - hotelProductStrategyDate.getDateBegin().toEpochDay();
                for (int i = 0; i <= diffDay; i++) {
                    localDateRuleInfoMap.put(hotelProductStrategyDate.getDateBegin().plusDays(i), airportExpressDetailDTO);
                }
            }

            for (LocalDate localDate : localDateRuleInfoMap.keySet()) {
                if(localDate.toString().substring(0,7).equals(yearAndMonth.toString().substring(0,7))){
                    localDateRuleInfoMap2.put(localDate,localDateRuleInfoMap.get(localDate));
                }
            }
        }
        TreeMap<LocalDate, HotelDetailDTO> localDateListTreeMap = new TreeMap<>(LocalDate::compareTo);
        localDateListTreeMap.putAll(localDateRuleInfoMap2);
        return localDateListTreeMap;
    }

    /**
     * 加幅渠道和加幅时间校验
     * @param hotelProductStrategyReqList
     * @return
     */
    private Result<Object> checkStrategyDateResult(List<HotelProductStrategyReq> hotelProductStrategyReqList) {
        List<HotelProductStrategyDateReq> hotelProductStrategyDateReqList = new ArrayList<>();
        // 四种渠道至少有一个在售校验
        for (HotelProductStrategyReq req : hotelProductStrategyReqList) {
            hotelProductStrategyDateReqList.addAll(req.getHotelProductStrategyDateReqList());

            if(SALE_CHANNEL_TYPE_DISABLED.equals(req.getSelfSalePermissionType())
                    && SALE_CHANNEL_TYPE_DISABLED.equals(req.getAgentSalePermissionType())){
                //如果线上渠道和线下渠道都禁用
                return Result.failed(SystemError.HOTEL_10018);
            }
            if(SALE_CHANNEL_TYPE_SELECTED.equals(req.getSelfSalePermissionType())){
                if(CollectionUtils.isEmpty(req.getHotelProductChannelOfflineSelfCustomReqList())
                        && CollectionUtils.isEmpty(req.getHotelProductChannelOnlineSelfPlatformReqList())){
                    return Result.failed(SystemError.HOTEL_10019);

                }
            }
            if(SALE_CHANNEL_TYPE_SELECTED.equals(req.getAgentSalePermissionType())){
                if(CollectionUtils.isEmpty(req.getHotelProductChannelOfflineAgentCustomReqList())
                        && CollectionUtils.isEmpty(req.getHotelProductChannelOnlineThirdPlatformReqList())){
                    return Result.failed(SystemError.HOTEL_10020);
                }
            }
        }
        for (int i = 0; i < hotelProductStrategyDateReqList.size(); i++) {
            HotelProductStrategyDateReq strategyDateReq = hotelProductStrategyDateReqList.get(i);
            if (strategyDateReq.getDateBegin().isBefore(LocalDate.now())) {
                // 酒店产品加幅策略保存，开始日期不能小于当前日期校验
                return Result.failed(SystemError.HOTEL_10021);
            }
            if (i == 0 && strategyDateReq.getDateBegin().isAfter(strategyDateReq.getDateEnd())) {
                // 开始日期必须小于结束日期校验
                return Result.failed(SystemError.HOTEL_10022);
            }
            for (int j = 0; j < hotelProductStrategyDateReqList.size(); j++) {
                HotelProductStrategyDateReq strategyDateReqTemp = hotelProductStrategyDateReqList.get(j);
                if (i >= j) { // 自身和已经校验过的跳过日期校验
                    continue;
                }
                if (i == 0 && strategyDateReqTemp.getDateBegin().isAfter(strategyDateReqTemp.getDateEnd())) {
                    // 开始日期必须小于结束日期校验
                    return Result.failed(SystemError.HOTEL_10023);
                }
                if (DateUtil.isDateRange(strategyDateReq.getDateBegin(), strategyDateReq.getDateEnd(), strategyDateReqTemp.getDateBegin(), strategyDateReqTemp.getDateEnd())) {
                    // 当前日期与其他日期区域重叠校验
                    return Result.failed(SystemError.HOTEL_10024);
                }
            }
        }
        return null;
    }

}
